const { userModel } = require('./models/adminsModels');
const mongoose = require("mongoose");
//登录
module.exports.login = async function (data) {
   let result = await userModel.find(data);
   return result;
}

//注册
module.exports.reg = async function (data) {
   let result = await userModel.create(data);
   return result;
}
//获取申请用户
module.exports.getApplyUser = async function () {
   let result = await userModel.find();
   return result;
}

//获取用户
module.exports.getUser = async ({
   currentPage = 0,
   pageSize = 5,
 }) => {
   const tableData = {
     currentPage: ~~currentPage,
     pageSize: ~~pageSize,
   }
   try {
     tableData.totalCount = await mongoose.model("userModel").find().count();
     tableData.users = await mongoose.model("userModel").find()
       .skip((tableData.currentPage - 1) * tableData.pageSize)
       .limit(tableData.pageSize)
       .populate("userId")
       .populate({
         path: "orderGoodsId",
         populate: {
           path: 'goodId'
         }
       })
     return tableData;
   } catch (e) {
     console.log('=============== getMovies 异常: =====================');
     console.log(e);
     console.log('====================================');
   }
 }
 
// module.exports.getUser = async function ({ currentPage, eachPage, userStatus, type, text }) {
//    let count, users
//    if (userStatus === undefined) {
//       if (text === undefined | type === "userStatus" | type === "userType") {
//          count = await userModel.find({ [type]: text }).countDocuments();  // 总条数
//          users = await userModel
//             .find({ [type]: text })
//             .skip((currentPage - 1) * eachPage)
//             .limit(eachPage - 0)
//       } else {
//          count = await userModel.find({ [type]: { $regex: text, $options: '$i' } }).countDocuments();  // 总条数
//          users = await userModel
//             .find({ [type]: { $regex: text, $options: '$i' } })
//             .skip((currentPage - 1) * eachPage)
//             .limit(eachPage - 0)
//       }
//    } else {
//       if (text === undefined) {
//          count = await userModel.find({ userStatus, [type]: text }).countDocuments();  // 总条数
//          users = await userModel
//             .find({ userStatus, [type]: text })
//             .skip((currentPage - 1) * eachPage)
//             .limit(eachPage - 0)
//       } else {
//          count = await userModel.find({ userStatus, [type]: { $regex: text, $options: '$i' } }).countDocuments();  // 总条数
//          users = await userModel
//             .find({ userStatus, [type]: { $regex: text, $options: '$i' } })
//             .skip((currentPage - 1) * eachPage)
//             .limit(eachPage - 0)
//       }
//    }
//    totalPage = Math.ceil(count / eachPage); // 总页数
//    let pageData = {
//       currentPage: currentPage - 0,  // 当前页码
//       eachPage: eachPage - 0,  // 每页显示条数
//       totalPage: totalPage - 0,   // 总页数
//       count: count - 0, // 总条数
//       users  // 用户数据
//    };

//    return pageData;
// }

//修改用户的信息
module.exports.UpdateUser = async function ({ _id, userName, userPhone, userPwd, userMail, userStatus, }) {
   return await userModel.updateOne({ _id }, { userName, userPhone, userPwd, userMail, userStatus, });
}

//修改用户状态
module.exports.auditPass = async function ({ _id, userStatus }) {
   return await userModel.updateOne({ _id }, { userStatus });
}

//删除用户
module.exports.removeUser = async function (id) {
   return await userModel.deleteOne(id);
}

//批量删除后台用户
module.exports.deletes = async function (id) {
   return await userModel.remove({ _id: { $in: id._id } });
}
//处理管理员申请，修改 userStatus 的状态值 
module.exports.updateUserStatus = async (data) => {
   const { _id, userStatus } = data
   return await userModel.updateOne({ _id } ,{ userStatus })
}

